﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Auditors_CompanyDirectors : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["CompanyDirectors"] = null;
            Bind();
        }

    }

    private List<CompanyDiectorsEntity> dt;

    private void GetData()
    {
        dt = Session["CompanyDirectors"] as List<CompanyDiectorsEntity>;

        if (dt == null)
        {
            string company = Request.QueryString["CompanyID"];

            dt = new CompanyBLL().GetCompanyDiectors(company);

            if (dt == null)
            {
                dt = new List<CompanyDiectorsEntity>();
            }
        }
    }

    private void Bind()
    {
        string OrderID = Request.QueryString["OrderID"];

        GetData();
        GridView1.DataSource = dt.Where(fun => fun.ISDirector == 0);
        GridView1.DataBind();

        GridView2.DataSource = dt.Where(fun => fun.ISDirector == 1);
        GridView2.DataBind();

        GridView3.DataSource = new CompanyBLL().CompanyAuditorsStatusLog(OrderID);
        GridView3.DataBind();

        string company = Request.QueryString["CompanyID"];

        Session["CompanyDirectors"] = dt;

        var v = new CompanyBLL().GetCompanyByCompanyid(company);
        lbCompanyID.Text = v.CompanyID;
        lbEnglishName.Text = v.EnglishName;
        lbChineseName.Text = v.ChineseName;
        lbStock.Text = v.Stock.ToString();
        lbRegisterDate.Text = v.RegisterDate.ToString("yyyy-MM-dd");
        lbHasProfitTax.Text = v.HasProfitTax ? "有" : "没有";



        var AuditorsEntity = new CompanyBLL().GetCompanyAuditorsEntityByOrderId(OrderID);
        if (AuditorsEntity != null)
        {
            TextBox8.Text = AuditorsEntity.BusinessScope;
            TextBox9.Text = AuditorsEntity.BusinessAddress;
            TextBox10.Text = AuditorsEntity.TotalTurnover.ToString();
            TextBox3.Text = AuditorsEntity.BR;
            TextBox4.Text = AuditorsEntity.StartTime.ToString("yyyy-MM-dd");
            TextBox5.Text = AuditorsEntity.EndTime.ToString("yyyy-MM-dd");
        }


        TextBox1.Text = new AuditorsBLL().GetProfitTax(company, OrderID).ToString();

    }

    private void GetProportion()
    {
        GetData();

        foreach (GridViewRow row in GridView1.Rows)
        {
            try
            {
                dt.First(fun => fun.DirectorName == row.Cells[0].Text).Proportion = Convert.ToDouble((row.Cells[2].FindControl("TextBox1") as TextBox).Text);
            }
            catch (Exception)
            {
                AlertMsg(string.Format("股东({0})占比格式不正确", row.Cells[0].Text));
                return;
            }
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }


    protected void Button4_Click(object sender, EventArgs e)
    {
        GetProportion();

        if (string.IsNullOrEmpty(TextBox2.Text.Trim()))
        {
            AlertMsg("董事名不能为空");
            return;
        }

        if (string.IsNullOrEmpty(TextBox7.Text.Trim()))
        {
            AlertMsg("董事英文名不能为空");
            return;
        }

        if (dt.Count(fun => fun.DirectorName == TextBox2.Text.Trim() && fun.ISDirector == 1) > 0)
        {
            AlertMsg("董事名已存在");
            return;
        }

        if (dt.Count(fun => fun.EnglishName == TextBox7.Text.Trim() && fun.ISDirector == 1) > 0)
        {
            AlertMsg("董事英文名已存在");
            return;
        }

        CompanyDiectorsEntity dr = new CompanyDiectorsEntity();

        dr.DirectorName = TextBox2.Text.Trim();
        dr.EnglishName = TextBox7.Text.Trim();
        dr.Proportion = 0.0;
        dr.ISDirector = 1;

        dt.Add(dr);

        Bind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        GetProportion();

        if (string.IsNullOrEmpty(txtName.Text.Trim()))
        {
            AlertMsg("股东名不能为空");
            return;
        }

        if (string.IsNullOrEmpty(TextBox6.Text.Trim()))
        {
            AlertMsg("股东英文名不能为空");
            return;
        }

        if (dt.Count(fun => fun.DirectorName == txtName.Text.Trim() && fun.ISDirector == 0) > 0)
        {
            AlertMsg("股东名已存在");
            return;
        }


        if (dt.Count(fun => fun.EnglishName == TextBox6.Text.Trim() && fun.ISDirector == 0) > 0)
        {
            AlertMsg("股东英文名已存在");
            return;
        }

        double f;

        try
        {
            f = Convert.ToDouble(txtProportion.Text.Trim());
        }
        catch (Exception)
        {
            AlertMsg("占比格式错误");
            return;
        }

        if (f > 100 || f <= 0)
        {
            AlertMsg("占比必须大于0且小于等于100");
            return;
        }

        CompanyDiectorsEntity dr = new CompanyDiectorsEntity();

        dr.DirectorName = txtName.Text.Trim();
        dr.EnglishName = TextBox6.Text.Trim();
        dr.Proportion = f;
        dr.ISDirector = 0;

        dt.Add(dr);

        Bind();
    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GetProportion();
        dt.Remove(dt.First(fun => fun.DirectorName.Equals(e.Keys[0]) && fun.ISDirector == 0));

        Bind();
    }

    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GetProportion();

        dt.Remove(dt.First(fun => fun.DirectorName.Equals(e.Keys[0]) && fun.ISDirector == 1));

        Bind();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        GetProportion();
        int d = 0;
        foreach (var dataRow in dt)
        {
            try
            {
                d += Convert.ToInt32(dataRow.Proportion) * 1000;
            }
            catch (Exception)
            {
                AlertMsg(string.Format("股东({0})占比格式不正确", dataRow.DirectorName));
                return;
            }
        }

        if (d / 1000 != 100)
        {
            AlertMsg("占比总和应为100%");
            return;
        }

        string company = Request.QueryString["CompanyID"];

        string orderid = Request.QueryString["orderid"];

        CompanyAuditorsEntity entity = new CompanyAuditorsEntity();

        entity.BR = TextBox3.Text;
        try
        {

            entity.StartTime = Convert.ToDateTime(TextBox4.Text);
        }
        catch (Exception)
        {
            AlertMsg("核实开始日期格式错误");
            return;
        }
        try
        {
            entity.EndTime = Convert.ToDateTime(TextBox5.Text);
        }
        catch (Exception)
        {
            AlertMsg("核实结束日期格式错误");
            return;
        }

        entity.CompanyId = company;
        entity.OrderId = orderid;
        entity.BusinessScope = TextBox8.Text;
        entity.BusinessAddress = TextBox9.Text;

        if (string.IsNullOrEmpty(TextBox10.Text))
        {
            entity.TotalTurnover = 0;
        }
        else
        {
            try
            {

                entity.TotalTurnover = Convert.ToInt32(TextBox10.Text);
            }
            catch (Exception)
            {
                entity.TotalTurnover = 0;
            }
        }


        new CompanyBLL().CreateCompanyDiectors(company, dt);

        new CompanyBLL().CreateCompanyAuditors(entity);

        try
        {

            new AuditorsBLL().InsertProfitTax(company, orderid, Convert.ToDouble(TextBox1.Text.Trim()));
        }
        catch (Exception)
        {
            AlertMsg("利得税格式错误，请重新输入");
            return;
        }


        Response.Redirect("SelectBank.aspx?CompanyID=" + company + "&orderid=" + orderid);
        //Response.Redirect("CompanyBankMantissa.aspx?CompanyID=" + company + "&orderid=" + orderid);

    }
    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int status = Convert.ToInt32(e.Row.Cells[2].Text);
            AuditorsStatus pStatus = (AuditorsStatus)status;
            e.Row.Cells[2].Text = RemarkAttribute.GetEnumRemark(pStatus);
        }
    }
}